package com.ssly.media.manage.service;

import com.ssly.media.manage.configuration.MediaProperties;
import com.ssly.media.manage.domain.ConvertTask;
import com.ssly.media.manage.domain.SourceMedia;
import com.ssly.media.manage.ffmpeg.Media;
import com.ssly.media.manage.ffmpeg.MediaConverter;
import com.ssly.media.manage.repository.ConvertTaskRepository;
import com.ssly.media.manage.repository.SourceMediaRepository;
import com.ssly.media.manage.repository.TargetMediaRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import sun.reflect.generics.repository.ConstructorRepository;

import java.io.File;
import java.sql.Timestamp;

import static java.lang.Thread.sleep;

@Service
public class ConvertTaskService {


    @Autowired
    private SourceMediaRepository sourceMediaRepository;
    @Autowired
    private TargetMediaService targetMediaService;
    @Autowired
    private ConvertTaskRepository convertTaskRepository;
    @Autowired
    private MediaProperties mediaProperties;

    public void create(ConvertTask convertTask){
        SourceMedia sourceMedia = sourceMediaRepository.findById(convertTask.getSourceMediaId()).get();

        convertTask.setName(sourceMedia.getName());
        convertTask.setStatus(0);
        convertTask.setCreateTime(new Timestamp(System.currentTimeMillis()));
        ConvertTask saveConvertTask = convertTaskRepository.save(convertTask);
//        源媒体名
        String sourceMediaName=sourceMedia.getName();
        sourceMediaName=sourceMediaName.substring(0,sourceMediaName.lastIndexOf("."));

        File sourceMediaFile=new File(sourceMedia.getSavePath());
//        源媒体文件名
        String sourceMediaFileName=sourceMediaFile.getName();
        String fileUUID=sourceMediaFileName.substring(0,sourceMediaFileName.lastIndexOf("."));
        File targetMediaFile=new File(mediaProperties.getTargetDir(),fileUUID+ ".ts");
        Media input = new Media(sourceMediaFile);
        Media ouput = new Media(targetMediaFile);
        ouput.setVcodec("h264");
        MediaConverter mediaConverter = new MediaConverter(input,ouput);
        new Thread(mediaConverter).start();
        String finalSourceMediaName = sourceMediaName;
        new Thread(()->{
            while (true){
                try {
                    sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                saveConvertTask.setProgress(mediaConverter.getProcess());
                convertTaskRepository.save(saveConvertTask);
                if (mediaConverter.getProcess()==100){
                    saveConvertTask.setStatus(1);
                    saveConvertTask.setCompleteTime(new Timestamp(System.currentTimeMillis()));
                    convertTaskRepository.save(saveConvertTask);
                    targetMediaService.create(targetMediaFile, finalSourceMediaName+ ".ts");
                    return;
                }
            }
        }).start();

    }
}
